package com.educal.Controller;

import com.educal.Repository.StuRepository;
import com.educal.Bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by miyang on 2017/12/6.
 */
@RestController
public class Admin_StuController {

    @Autowired
    private StuRepository stuRepository;
    /*
    * 增加学生*/
   @PostMapping(value = "/students")
   public Map<String,Object> addStu(@RequestParam("stuId") String stuId,
                                    /*@RequestParam("stuPassword") String stuPassword,*/
                                    @RequestParam("stuName") String stuName,
                                    @RequestParam("stuCollege") String stuCollege,
                                    @RequestParam("stuClass") String stuClass){
       Student stu = new Student();
       Map<String, Object> result = new HashMap<String, Object>();
       stu.setStuid(stuId);
       stu.setStupassword("123456");
       stu.setStuname(stuName);
       stu.setStucollege(stuCollege);
       stu.setStuclass(stuClass);
       stu = stuRepository.save(stu);

       if(stu!=null) {
           result.put("code", 200);
           result.put("stu", stu);
       }else{
           result.put("code", 400);
       }
        //System.out.print(stu.getStuid()+ stu.getStuname());
       return result;
    }

    /*
    * 查询全部学生信息*/
    @GetMapping(value= "/students")
    public Map<String, Object> findAll() {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Student> stuList = stuRepository.findAll();
        if(stuList!=null&&!stuList.isEmpty()){
            result.put("code",200);
            result.put("stuList",stuList);
        }else{
            result.put("code",400);
        }
        return result;
    }

    /*
    * 用id查询 唯一查询*/
    @GetMapping(value = "/students/{stuId}")
    public Map<String, Object> findByStuId(@PathVariable("stuId") String stuId) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code",400);
        Student stu = new Student();
        List<Student> list = stuRepository.findAll();
        for(int i=0;i<list.size();i++){
            String sId = list.get(i).getStuid();
            if(sId.equals(stuId)){
                stu.setStuid(sId);
                stu.setStupassword(list.get(i).getStupassword());
                stu.setStuname(list.get(i).getStuname());
                stu.setStucollege(list.get(i).getStucollege());
                stu.setStuclass(list.get(i).getStuclass());
                result.put("code",200);
                result.put("stu",stu);
                break;
            }
        }
        return result;
    }
    /*
    * 用name查询 列表查询*/
    @GetMapping(value = "/students/name/{stuName}")
    public Map<String,Object> findByName(@PathVariable("stuName") String stuName) {
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Student> stuList = new ArrayList<>();
        List<Student> list = stuRepository.findAll();
        for(int i=0;i<list.size();i++){
            String sName = list.get(i).getStuname();
            if(sName.equals(stuName)){
                stuList.add(list.get(i));
            }
        }
        if(stuList!=null&&!stuList.isEmpty()){
            result.put("code",200);
            result.put("stuList",stuList);
        }
        // result.put("stuList",stuList);
        return result;
    }
    /*
    * 用班级查询 列表查询*/
    @GetMapping(value = "/students/class/{stuClass}")
    public Map<String,Object> findByClass(@PathVariable("stuClass") String stuClass) {
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Student> list = stuRepository.findAll();
        List<Student> stuList = new ArrayList<>();
       // System.out.print(list);
        for(int i=0;i<list.size();i++){
            String sClass = list.get(i).getStuclass();
            if(sClass.equals(stuClass)) {
                stuList.add(list.get(i));
            }
        }
        if(stuList!=null&&!stuList.isEmpty()){
            result.put("code",200);
            result.put("stuList",stuList);
        }
       // result.put("stuList",stuList);
        return result;
    }


    /*
    * 修改学生信息*/
    @PutMapping(value = "/students")
    public Map<String, Object> updateStuId(@RequestParam("stuId") String stuId,
                               @RequestParam("stuPassword") String stuPassword,
                               @RequestParam("stuName") String stuName,
                               @RequestParam("stuCollege") String stuCollege,
                               @RequestParam("stuClass") String stuClass){
        Student stu = new Student();
        Map<String, Object> result = new HashMap<String, Object>();
        stu.setStuid(stuId);
        stu.setStupassword(stuPassword);
        stu.setStuname(stuName);
        stu.setStucollege(stuCollege);
        stu.setStuclass(stuClass);
        stu = stuRepository.save(stu);

        if(stu!=null) {
            result.put("code", 200);
            result.put("stu", stu);
        }else{
            result.put("code", 400);
        }
        //System.out.print(stu.getStuid()+ stu.getStuname());
        return result;
    }

    /*
    * 用id删除学生*/
    @DeleteMapping(value = "/students/{stuId}")
    public Map<String,Object> deleteStu(@PathVariable("stuId") String stuId){
        Student stu = new Student();
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Student> list = stuRepository.findAll();
        for(int i=0;i<list.size();i++){
            String sId = list.get(i).getStuid();
            if(sId.equals(stuId)){
                stu.setStuid(sId);
                stu.setStupassword(list.get(i).getStupassword());
                stu.setStuname(list.get(i).getStuname());
                stu.setStucollege(list.get(i).getStucollege());
                stu.setStuclass(list.get(i).getStuclass());
                stuRepository.delete(stu);
                result.put("code",200);
                break;
            }
        }
        return result;
    }
}
